"""
主机版本查询查询
测试连接
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import time
import logging.config
from conf import settings
from lib.common import wait_table

logging.config.dictConfig(settings.LOGGING_DIC)  # 导入上面定义的logging配置
logger = logging.getLogger("测试连接")  # 生成一个测试连接log实例

def link_test_interface(browser, wait, host_row, host):
    if host_row is None:
        logger.error(f"主机 {host.ip} 存在问题,中止测试连接")
        return
    if host.sync_status and "正在" in host.sync_status:
        host.connect_status = "状态异常"
        host.is_bad = True
        host.save()
        logger.error("状态异常，未能进行测试")
        return

    try:
        host_row.click()
        browser.find_element(By.XPATH, '//*[@id="testConnection"]').click()

        browser.switch_to.frame(1)

        browser.find_element(By.XPATH, '//*[@id="id0"]').click()

        text = wait.until(EC.presence_of_element_located((
            By.XPATH, '//*[@id="dialog-message"]'
        ))).text
        print(text)
        host.connect_status = text
        if text != "连接成功":
            host.is_bad = True
        host.save()
    except NoSuchElementException:
        host.connect_status = "操作元素未找到"
        host.is_bad = True
        host.save()
        logger.error("NoSuchElement操作元素未找到，请联系开发者")
    except TimeoutException:
        host.connect_status = "测试连接超时"
        host.is_bad = True
        host.save()
        logger.error("Timeout测试连接超时")
    browser.switch_to.parent_frame()
    browser.find_element(By.XPATH, '/html/body/div[6]/div[1]/a/span').click()
